import { Module, Logger } from '@nestjs/common'
import { DbService } from './db.service'
import { MongooseModule } from '@nestjs/mongoose'
import config from '../../../src/config'

const { protocol, dbName, host, userName, password, port, ssl } = config.db
// 通过admin认证
const URI = `${protocol}://${userName}:${password}@${host}${
  port ? ':' + port : ''
}/${dbName}?authSource=admin&readPreference=primary&ssl=${ssl + ''}`
// 日志
Logger.log(URI)
Logger.log(`info ${process.env.NODE_ENV} server port ${config.server.port}`)
@Module({
  imports: [MongooseModule.forRoot(URI, { useFindAndModify: false })],
  providers: [DbService],
  exports: [DbService],
})
export class DbModule {}
